import numpy as np

# 假设状态空间的大小和动作空间的大小
state_size = 100  # 根据实际情况设置
action_size = 5   # 代表生产g1到g5的决策

# 初始化Q-table，每一行包含的字段
# 假设我们将每个字段展平并连接到一个一维数组中
q_table = np.zeros((state_size, 10 + 12 * action_size))  # 10个指标 + 每个产品的12个信息（生产量、合格率、价格、成本、利润）

# 定义字段索引
total_profit_index = 0
total_production_index = 1
average_quality_index = 2
average_price_index = 3
average_cost_index = 4

# 每个工厂每个月每种产品的详细信息
monthly_info_offset = 5  # 从第5列开始
for month in range(12):  # 12个月
    for product in range(action_size):  # 每种产品
        q_table[:, monthly_info_offset + month * action_size + product] = 0  # 初始化每个月每种产品的信息

# 示例：打印初始化的Q-table结构
print("初始Q-table:")
print(q_table)

def update_q_table(q_table, state, action, total_profit, total_production, average_quality, average_price, average_cost, monthly_info):
    # 更新总利润、总生产量、平均合格率、平均价格和平均成本
    q_table[state][total_profit_index] = total_profit
    q_table[state][total_production_index] = total_production
    q_table[state][average_quality_index] = average_quality
    q_table[state][average_price_index] = average_price
    q_table[state][average_cost_index] = average_cost

    # 更新每个月每种产品的信息
    start_index = monthly_info_offset
    for month in range(12):
        for product in range(action_size):
            q_table[state][start_index + month * action_size + product] = monthly_info[month][product]

# 示例更新
state = 0  # 当前状态
action = 1  # 当前选择的动作
total_profit = 50000
total_production = 1000
average_quality = 0.85
average_price = 30
average_cost = 20
monthly_info = [[50, 60, 70, 80, 90] for _ in range(12)]  # 每个月每种产品的生产量

# 更新Q-table
update_q_table(q_table, state, action, total_profit, total_production, average_quality, average_price, average_cost, monthly_info)
print("更新后的Q-table:")
print(q_table)
